/*
 * SoyEditor.cpp
 *
 *  Created on: 2012-9-15
 *      Author: yangentao@gmail.com
 */

#include "Editor.h"

const char* testText = "hello \n YangEntao \nbye!";



Editor::Editor() {
	addMessageListener(&adapter);
	adapter.OnPaint = boost::bind(&Editor::onPaint, this, _1, _2, _3);
	doc.setText(testText);
	Super::addStyle(WS_HSCROLL | WS_VSCROLL);
}

Editor::~Editor() {
}

bool Editor::create(HWND parent, Rect rect) {
	WindowParam param(true);
	param.ChildParam(parent, rect);
	param.exStyle |= WS_EX_CLIENTEDGE;
	return Super::createChild(param);
}

void Editor::onPaint(HWND w, HDC dc, PAINTSTRUCT* ps) {
//	TextOutA(dc, 20, 20, "Hello Editor", 5);
	Canvas c(dc);
	int lineCount = doc.getLineCount();
	for (int i = 0; i < lineCount; ++i) {
		String lineText = doc.getLineText(i);
		drawLine(c, i, lineText);
	}
}

void Editor::drawLine(Canvas& c, int line, String& lineText) {
	Rect clientRect = getClientRect();
	int textHeight = c.textHeight();

	int vPos = GetScrollPos(getWnd(), SB_VERT);
	int lineY = line * textHeight;
	c.moveTo(0, lineY);
	Rect r = clientRect;
	r.top = lineY;
	r.bottom = r.top + textHeight;
	int ybase = r.bottom - 4;
	c.drawTextClipped(r, ybase, lineText);
}

void Editor::openFile(String filename) {

}
void Editor::setText(String text) {
	doc.setText(text);
}
String Editor::getText() {
	return doc.getText();
}
